//
//  CQVisitorView.swift
//  weibo
//
//  Created by chengqiang on 16/7/18.
//  Copyright © 2016年 chengqiangBiaodashi. All rights reserved.
//

import UIKit



//MARK: - 为点击事件自定义代理方法
    //定义代理协议
protocol VistorViewDelegate:NSObjectProtocol{
    //定义代理方法
    func VistitorDidLoginClick()
    func VistiorDidRegisterClick()
}
class CQVisitorView: UIView {
 //定义代理方法属性
    weak var delegate:VistorViewDelegate?
    
    
    
///开启能从xib或storyboard加载 系统默认能用代码创建也能直接拖控件
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor=UIColor(white: 237/255.0, alpha: 1)
        self.setMainUILayout()
    }
    
    
    func setUI(ImageName:String,Lable:String){
        self.houseView.hidden=true
        self.sendSubviewToBack(converImage)
        self.WhilesIcon.image=UIImage(named: ImageName)
        self.messagelable.text=Lable
    }
    
    /// private 私有方法 拒绝外部访问
    
//MARK:-设置界面布局
    private func setMainUILayout()
    {
        
        ///添加子控件
        self.addSubview(WhilesIcon)
        self.addSubview(converImage)
        self.addSubview(houseView)
        self.addSubview(messagelable)
        self.addSubview(regisetorBtn)
        self.addSubview(loginBtn)
        
        
        ///纯代码设置约束需要关闭Autoresizing的影响
        WhilesIcon.translatesAutoresizingMaskIntoConstraints=false
        converImage.translatesAutoresizingMaskIntoConstraints=false
        houseView.translatesAutoresizingMaskIntoConstraints=false
        messagelable.translatesAutoresizingMaskIntoConstraints=false
        regisetorBtn.translatesAutoresizingMaskIntoConstraints=false
        loginBtn.translatesAutoresizingMaskIntoConstraints=false
        
        
            // MARK:- 设置控件约束
        ///中间图
        self.addConstraint(NSLayoutConstraint(item: WhilesIcon, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: WhilesIcon, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        
        ///遮罩
        self.addConstraint(NSLayoutConstraint(item: converImage, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: WhilesIcon, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: converImage, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: WhilesIcon, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        ///中心图
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: WhilesIcon, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        ///文本信息
        self.addConstraint(NSLayoutConstraint(item: messagelable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: messagelable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: WhilesIcon, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: messagelable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 260))
        
        ///注册按钮
        self.addConstraint(NSLayoutConstraint(item: regisetorBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem:messagelable , attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: regisetorBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messagelable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: regisetorBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: regisetorBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        ///登录按钮
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messagelable, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messagelable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
    }
    
    //MARK:- 控件懒加载
    ///转图
    private lazy var WhilesIcon:UIImageView={
        let image = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return image
    }()
    ///遮罩  
    private lazy var converImage:UIImageView={
        let image=UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return image
    }()
    
    ///房子图片
    private lazy var houseView:UIImageView=UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    ///添加中间文字
    private lazy var messagelable:UILabel={
    let lable=UILabel(frame: CGRectZero)
        
        lable.textAlignment=NSTextAlignment.Center
        lable.font=UIFont.systemFontOfSize(15)
        lable.textColor=UIColor.darkGrayColor()
        lable.numberOfLines=0
        lable.text="关注一些人,看看有什么惊喜!"
        
        ///根据自己内容调整
        lable.sizeToFit()
        
        return lable
    }()
 
    ///注册按钮
    private lazy var regisetorBtn:UIButton={
    let button = UIButton(type: UIButtonType.Custom)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.titleLabel?.font=UIFont.systemFontOfSize(18)
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        button.addTarget(self, action: "regersiterClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    ///登录按钮
    private lazy var loginBtn:UIButton={
        let  button = UIButton(type: UIButtonType.Custom)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.titleLabel?.font=UIFont.systemFontOfSize(18)
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        button.addTarget(self, action: "logincClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        
    }()
    
    
    //MARK: - 添加控件对应的方法和事件
    
    //MARK:-为按钮绑定代理方法
    //登录
    func logincClick()  {
        delegate?.VistitorDidLoginClick()
    }
    //注册
    func regersiterClick() {
        delegate?.VistiorDidRegisterClick()
    }
    ///主页旋转图片
    func trainformImage(){        
        ///创建核心旋转动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        ///旋转角度
        animation.toValue=2*M_PI
        ///持续时间
        animation.duration=20
        ///持续次数
        animation.repeatCount=MAXFLOAT
        ///关闭释放动画 让动画一直都在
        animation.removedOnCompletion=false
        ///添加动画
        WhilesIcon.layer.addAnimation(animation, forKey: nil)
    }
   
}
